package com.dog.basics.api.controller.common;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.RandomUtil;
import com.dog.basics.bean.dto.api.common.BdUserDto;
import com.dog.basics.bean.dto.api.common.UserRegisterDto;
import com.dog.basics.bean.dto.api.common.WxPhoneDto;
import com.dog.basics.bean.vo.admin.common.MiniUserInfoUUidVo;
import com.dog.basics.common.constant.SwaggerConstants;
import com.dog.basics.common.util.R;
import com.dog.basics.security.annotation.Inner;
import com.dog.basics.service.common.IDogEmialService;
import com.dog.basics.service.common.IDogUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;


@RestController
@RequestMapping("/api/login")
@Api(value = "用户登陆注册", tags = SwaggerConstants.API_ALL)
@AllArgsConstructor
public class ApiLoginController {
    private final IDogUserService userService;
    private final IDogEmialService mailService;

    /**
     * 授权获取微信手机号码
     *
     * @param dto
     * @return guonh 2020年3月6日
     */
    @ApiOperation(value = "获取微信手机号", notes = "授权获取微信手机号码", tags = SwaggerConstants.API_MY)
    @GetMapping("/wxPhone")
    public R<Boolean> getWxPhone(WxPhoneDto dto) {
        return R.ok(userService.getWxPhone(dto));
    }

    /**
     * 获取商城微信手机号
     *
     * @param dto
     * @return guonh 2020年3月6日
     */
    @ApiOperation(value = "获取商城微信手机号", notes = "获取商城微信手机号", tags = SwaggerConstants.API_MY)
    @GetMapping("/wxShopPhone")
    public R<Boolean> wxShopPhone(WxPhoneDto dto) {
        return R.ok(userService.wxShopPhone(dto));
    }

    /**
     * 获取微信uuid
     *
     * @param dto
     * @return guonh 2020年3月6日
     */
    @ApiOperation(value = "获取微信uuid", notes = "获取微信uuid", tags = SwaggerConstants.API_MY)
    @GetMapping("/gewxUuId")
    @Inner
    public R<MiniUserInfoUUidVo> gewxUuId(WxPhoneDto dto) {
        return R.ok(userService.gewxUuId(dto));
    }

    /**
     * 获取微信uuid 商城
     *
     * @param dto
     * @return guonh 2020年3月6日
     */
    @ApiOperation(value = "获取微信uuid 商城", notes = "获取微信uuid 商城", tags = SwaggerConstants.API_MY)
    @GetMapping("/gewxShopUuId")
    @Inner
    public R<MiniUserInfoUUidVo> gewxShopUuId(WxPhoneDto dto) {
        return R.ok(userService.gewxShopUuId(dto));
    }
    /**
     * 获取微信openId
     * @param dto
     * @return
     *  guonh 2020年3月6日q
     */
/*    @ApiOperation(value = "获取微信uuId",notes = "获取微信uuId",tags = SwaggerConstants.API_MY )
    @GetMapping("/wxOpenId")
    public R<Boolean> getwxOpenId(WxOpenIdDto dto){
        return R.ok(userService.gewxOpenId(dto));
    };*/

    /**
     * @Description: 用户注册(手机号或者email)
     * @Param: userRegisterDto
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @Inner
    @PostMapping("/registerOrEmail")
    @ApiOperation(value = "用户注册(手机号或者email)", notes = "用户注册(手机号或者email)", tags = SwaggerConstants.API_LOGIN)
    @ApiIgnore
    public R<Boolean> registerOrEmail(@Valid @RequestBody UserRegisterDto userRegisterDto) {
        return R.ok(userService.registerOrEmail(userRegisterDto));
    }

    /**
     * @Description: 邮箱注册发送验证码
     * @Param:
     * @return: UserInfoVo
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @Inner
    @ApiOperation(value = "邮箱注册发送验证码", notes = "邮箱注册发送验证码", tags = SwaggerConstants.API_LOGIN)
    @ApiImplicitParam(name = "email", value = "邮箱", required = true, dataType = "String")
    @GetMapping("/emailSend/{email}")
    @ApiIgnore
    public R<Boolean> emailSend(@PathVariable("email") String email) {
        Validator.validateTrue(Validator.isEmail(email), "邮箱格式不对!");
        //生成验证码
        String captcha = RandomUtil.randomNumbers(4);
        return R.ok(mailService.sendSimpleMail(email, "邮箱注册验证码", captcha));
    }

    /**
     * 手机号注册发送验证码
     *
     * @param phone
     * @return guonh 2019年12月21日
     */
    @Inner
    @ApiOperation(value = "手机号注册发送验证码", notes = "手机号注册发送验证码", tags = SwaggerConstants.API_LOGIN)
    @ApiImplicitParam(name = "phone", value = "手机号码", required = true, paramType = "String")
    @GetMapping("/api/sendRgPhoneSms")
    @ApiIgnore
    public R<Boolean> sendRgPhoneSms(@RequestParam String phone) {
        return R.ok(userService.sendRegisterSms(phone));
    }

    /**
     * 短信登录发送验证码
     *
     * @param phone
     * @return guonh 2019年12月21日
     */
    @Inner
    @ApiOperation(value = "短信登录发送验证码", notes = "短信登录发送验证码", tags = SwaggerConstants.API_LOGIN)
    @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String")
    @GetMapping("/api/sendPhoneSms")
    public R<Boolean> sendPhoneSms(@RequestParam String phone) {
        return R.ok(userService.sendPhoneSms(phone));
    }


    /**
     * 忘记密码发送验证码
     *
     * @param phone
     * @return guonh 2019年12月21日
     */
    @Inner
    @ApiOperation(value = "忘记密码发送验证码", notes = "忘记密码发送验证码", tags = SwaggerConstants.API_LOGIN)
    @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String")
    @GetMapping("/api/sendforgetSms")
    @ApiIgnore
    public R<Boolean> sendforgetSms(@RequestParam String phone) {
        return R.ok(userService.sendforgetSms(phone));
    }

    /**
     * 绑定手机号发送验证码
     *
     * @param phone
     * @return guonh 2019年12月21日
     */
    @Inner
    @ApiOperation(value = "绑定手机号发送验证码", notes = "绑定手机号发送验证码", tags = SwaggerConstants.API_LOGIN)
    @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataType = "String")
    @GetMapping("/api/sendBdSms")
    public R<Boolean> sendBdSms(@RequestParam String phone) {
        return R.ok(userService.sendBdSms(phone));
    }

    /**
     * @Description: 忘记密码
     * @Param: oldPhone, password, code
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @Inner
    @ApiOperation(value = "忘记密码", notes = "忘记密码", tags = SwaggerConstants.API_LOGIN)
    @PutMapping("/forgetPassword")
    @ApiIgnore
    public R<Boolean> forgetPassword(@Valid @RequestBody UserRegisterDto userRegisterDto) {
        return R.ok(userService.forgetPassword(userRegisterDto));
    }

    /**
     * @Description: 绑定手机号
     * @Param: bdUserDto
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @Inner
    @ApiOperation(value = "绑定手机号", notes = "绑定手机号", tags = SwaggerConstants.API_LOGIN)
    @PutMapping("/bdUser")
    @ApiIgnore
    public R<Boolean> bdUser(@Valid @RequestBody BdUserDto bdUserDto) {
        return R.ok(userService.bdUser(bdUserDto));
    }


}
